package elitech.vietnam.weatherfashion.backgroundtask;

import android.content.Context;
import android.location.Location;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.SplashScreenActivity;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.ultilities.MyLocation;
import elitech.vietnam.weatherfashion.ultilities.ValueOption;
import elitech.vietnam.weatherfashion.weatherservice.WeatherObject;
import elitech.vietnam.weatherfashion.weatherservice.WeatherService;

public class GetWeatherTask extends AsyncTask<String, Integer, Boolean> {

	public static boolean gettingWeather;
	public static boolean gettingGPS;
	private Context context;
	private WeatherObject weather = null;
	private String myLatitude;
	private String myLongtitude;

	public GetWeatherTask(Context context) {
		this.context = context;
		gettingWeather = true;
		gettingGPS = false;
	}

	@Override
	protected Boolean doInBackground(String... params) {

		// cho lay GPS xong
		while (gettingGPS) {
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		ValueOption option = GlobalValue.getInstance(context).readOptionInfo();

		Log.i("GPS-Weather", "Start get weather");
		if (myLatitude.equals("") && myLongtitude.equals("") && option.isGetLocation()) {

			publishProgress(0);
		}

		WeatherService appWService = new WeatherService();
		if ((!myLatitude.equals("") || !myLongtitude.equals("")) && option.isGetLocation()) {

			Log.i("GPS-Weather", "GPS lat " + myLatitude);
			Log.i("GPS-Weather", "GPS lon " + myLongtitude);
			WeatherObject temp = appWService.getWeather(String.valueOf(myLatitude), String.valueOf(myLongtitude));
			if (temp != null && !temp.getListDay().isEmpty())
				weather = temp;

		} else {
			Log.i("GPS-Weather", "DEFAULT_CITY" + String.valueOf(Const.DEFAULT_CITY));
			WeatherObject temp = appWService.getWeather(Const.DEFAULT_CITY);
			if (temp != null && !temp.getListDay().isEmpty())
				weather = temp;
		}

		gettingWeather = false;

		Log.i("GPS-Weather", "Stop get weather");
		return true;
	}

	@Override
	protected void onPreExecute() {
		myLatitude = GlobalValue.getInstance(context).readLatitude();
		myLongtitude = GlobalValue.getInstance(context).readLongtitude();

		super.onPreExecute();
		gettingWeather = true;
		ValueOption option = GlobalValue.getInstance(context).readOptionInfo();
		if (option.isGetLocation()) {
			gettingGPS = true;
			// Utilities.checkGpsState(context);

			try {
				MyLocation myLocation = new MyLocation();
				Log.i("GPS-Weather", "Start get GPS");
				myLocation.getLocation(context, new MyLocation.LocationResult() {
					@Override
					public void gotLocation(Location location) {

						if (location != null) {
							myLatitude = String.valueOf(location.getLatitude());
							myLongtitude = String.valueOf(location.getLongitude());

							GlobalValue.getInstance(context).writeLatitude(myLatitude);
							GlobalValue.getInstance(context).writeLongtitude(myLongtitude);

							// AppConfig.getInstance().isGotLoc = true;
							Utils.Log(myLatitude + "\t" + myLongtitude);
						} else {}
						Log.i("GPS-Weather", "Stop get GPS " + location);
						gettingGPS = false;
					}
				});
			} catch (Exception e) {
				Utils.Log(e.getMessage());
				gettingGPS = false;
			}
		}
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);

		Toast.makeText(context, context.getResources().getString(R.string.weather_cannot_get_gps), Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onPostExecute(Boolean result) {
		GlobalValue.getInstance(context).writeWeatherInfo(weather);
		((SplashScreenActivity) context).finishLoading();
	}
}
